package com.kcs.fxmargin.ws;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import org.apache.log4j.Logger;

import th.co.kcs.www.schema.CBS.InquiryAccountDetailRequestType;
import th.co.kcs.www.schema.CBS.InquiryAccountDetailResponseType;
import th.co.kcs.www.schema.CBS.InquiryAccountStatementRequestType;
import th.co.kcs.www.schema.CBS.InquiryAccountStatementResponseType;
import th.co.kcs.www.schema.CBS.InquiryAccountStatementResponseTypeInquiryAccountStatementRespData;
import th.co.kcs.www.schema.CBS.InquiryAccountStatementResponseTypeInquiryAccountStatementRespDataAccountStatementsAccountStatement;
import th.co.kcs.www.schema.CBS.RequestHeaderType;
import th.co.kcs.www.wsdl.AccountServices.AccountServicesPortTypeProxy;

import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.commonUtils;
import com.kcs.fxmargin.bo.CheckLimitOrderBO;
import com.kcs.fxmargin.bo.InquiryAccountWSBO;
import com.kcs.fxmargin.dao.CheckLimitOrderDAO;
import com.kcs.fxmargin.model.MCustInfo;

public class InquiryAccountWS {
	
	private Logger log = Logger.getLogger(Constant.LOG_CBS);
	
	public InquiryAccountWSBO inquiryAccountDetail(InquiryAccountWSBO bo)throws Exception{
		AccountServicesPortTypeProxy proxy = null;
		InquiryAccountDetailRequestType request = null;
		InquiryAccountDetailResponseType response = null;
		RequestHeaderType header = null;
		String channelId = null;
		String tranDate = null;
		String transTime = null;
		int refId = 0;
		String acctId = null;
		
		log.info("Begin : InquiryAccountWS : inquiryAccountDetail");
		log.info("by user : "+bo.getUser());
		
		try{
			acctId = bo.getMarginAccNo();refId = Integer.parseInt(bo.getCifNo());
			acctId = bo.getMarginAccNo();
			channelId = "FX";
			tranDate = getYYYYMMDDToday(0);
			transTime = getHHMMSSNow();
			refId = Integer.parseInt(bo.getCifNo());
			acctId = bo.getMarginAccNo();
			
			header = new RequestHeaderType(channelId, tranDate, transTime, refId);

			request = new InquiryAccountDetailRequestType(header,acctId);
			
			proxy = new AccountServicesPortTypeProxy();
			
			response = proxy.inquiryAccountDetail(request);
			
			BigInteger responseCode = response.getInquiryAccountDetailRespHeader().getResponseCode();
			String responseDesc		= response.getInquiryAccountDetailRespHeader().getResponseDesc();
			
			log.info("InquiryAccountDetail : responseCode = " + responseCode);
			log.info("InquiryAccountDetail : responseDesc = " + responseDesc);
			
			if(Integer.parseInt(responseCode + "") == 0){
				log.info("InquiryAccountDetail data size = 1");
			}
			
			bo.setOutsBal(response.getOutsBal());
			
			
		}catch(Exception e){
			log.error(e.toString());
			throw e;
		}
		
		log.info("End : InquiryAccountWS : inquiryAccountDetail");
		return bo;
	}
	
	public InquiryAccountWSBO inquiryAccountStatement(InquiryAccountWSBO bo)throws Exception{
		AccountServicesPortTypeProxy proxy = null;
		InquiryAccountStatementRequestType request = null;
		InquiryAccountStatementResponseType response = null;
	    InquiryAccountStatementResponseTypeInquiryAccountStatementRespData data = null;
		RequestHeaderType header = null;
		String channelId = null;
		String tranDate = null;
		String transTime = null;
		int refId = 0;
		String acctId = null;
		String rangeStartDt = null;
		String rangeEndDt = null;
		BigDecimal rangeStartTransAmt = null;
		BigDecimal rangeEndTransAmt = null;
		String lastAcctId = null;
		Integer lastSeq = null;
		InquiryAccountStatementResponseTypeInquiryAccountStatementRespDataAccountStatementsAccountStatement[] ar = null;
		List<CheckLimitOrderBO> accountStatementDataList = null;
		CheckLimitOrderBO dataBo = null;
		
		log.info("Begin : InquiryAccountWS : inquiryAccountStatement");
		log.info("by user : "+bo.getUser());
		
		try{
			
			if(bo.getStartDate().equals("")){
				bo.setStartDate(getYYYYMMDDToday(-1));
			}
			
			if(bo.getEndDate().equals("")){
				bo.setEndDate(getYYYYMMDDToday(0));
			}
			

			channelId = "FX";
			tranDate = getYYYYMMDDToday(0);
			transTime = getHHMMSSNow();
			refId = Integer.parseInt(bo.getCifNo());
			acctId = bo.getMarginAccNo();
			rangeStartDt = bo.getStartDate();
			rangeEndDt = bo.getEndDate();
			rangeStartTransAmt = new BigDecimal(0);
			rangeEndTransAmt = new BigDecimal(9999999999.99);
			lastAcctId = "";
			lastSeq = new Integer(0);
			
			//*** --- For test //
//			channelId = "FX";
//			tranDate = "20131014";
//			transTime = "10:00:00";
//			refId = 12345;
//			acctId = "6054064";
//			rangeStartDt = "20131014";
//			rangeEndDt = "20131014";
//			rangeStartTransAmt = new BigDecimal(0);
//			rangeEndTransAmt = new BigDecimal(999999999.99);
//			lastAcctId = "";
//			lastSeq = new Integer(0);
			//-------//
			
			log.info(">channelId="+channelId);
			log.info(">tranDate="+tranDate);
			log.info(">transTime="+transTime);
			log.info(">refId="+refId);
			log.info(">acctId="+acctId);
			log.info(">acctId="+acctId);
			log.info(">rangeStartDt="+rangeStartDt);
			log.info(">rangeEndDt="+rangeEndDt);
			log.info(">rangeStartTransAmt="+rangeStartTransAmt);
			log.info(">rangeEndTransAmt="+rangeEndTransAmt);
			log.info(">lastAcctId="+lastAcctId);
			log.info(">lastSeq="+lastSeq);
			
			header = new RequestHeaderType(channelId, tranDate, transTime, refId);
			
			request = new InquiryAccountStatementRequestType(header,acctId,rangeStartDt,rangeEndDt,rangeStartTransAmt,rangeEndTransAmt,lastAcctId,lastSeq);
			
			proxy = new AccountServicesPortTypeProxy();
			
			response = proxy.inquiryAccountStatement(request);
			
			data = response.getInquiryAccountStatementRespData();
			
			if(data != null){
				ar = data.getAccountStatements();
			}
			
			BigInteger responseCode = response.getInquiryAccountStatementRespHeader().getResponseCode();
			String responseDesc		= response.getInquiryAccountStatementRespHeader().getResponseDesc();
			
			log.info("InquiryAccountStatement : responseCode = " + responseCode);
			log.info("InquiryAccountStatement : responseDesc = " + responseDesc);
			
			System.out.println("InquiryAccountStatement : responseCode = " + responseCode);//***
			System.out.println("InquiryAccountStatement : responseDesc = " + responseDesc);//***
			
			
			if(ar != null){
				accountStatementDataList = new ArrayList<CheckLimitOrderBO>();
				
				if(Integer.parseInt(responseCode + "") == 0){
					log.info("InquiryAccountStatement data size = " + ar.length);
					
					System.out.println("InquiryAccountStatement data size = " + ar.length);//***
				}
				
				for(int i=0; i<ar.length; i++){
					dataBo = new CheckLimitOrderBO();
					
					dataBo.setDrCrFlg(ar[i].getDrCrFlg());
					dataBo.setTransAmt(ar[i].getTransAmt());
					dataBo.setTransRefNo(ar[i].getTransRefNo());
					dataBo.setTransCode(ar[i].getTransCode());
					dataBo.setTransDt(ar[i].getTransDt());
					dataBo.setTransTime(ar[i].getTransTime());
					dataBo.setEffDt(ar[i].getEffDt());
					accountStatementDataList.add(dataBo);
				}
				
				bo.setAccountStatementDataList(accountStatementDataList);
			}
			
			
		}catch(Exception e){
			log.error(e.toString());
			throw e;
		}
		
		log.info("End : InquiryAccountWS : inquiryAccountStatement");
		return bo;
	}
	
	private String getYYYYMMDDToday(int diffDate){
		Locale locale = new Locale("us","US");
		Calendar c = Calendar.getInstance(locale);
		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DATE) + diffDate;
		
		return (y+"") + (m<10?"0"+m:m+"") + (d<10?"0"+d:d+"");
	}
	
	private String getHHMMSSNow(){
		Locale locale = new Locale("us","US");
		Calendar c = Calendar.getInstance(locale);
		int h = c.get(Calendar.HOUR);
		int m = c.get(Calendar.MINUTE);
		int s = c.get(Calendar.SECOND);
		
		return (h<10?"0"+h:h+"") + (m<10?"0"+m:m+"") + (s<10?"0"+s:s+"");
	}
	
	public void inquiryAccountStatementTest(InquiryAccountWSBO wsBo)throws Exception{
		List<CheckLimitOrderBO> accountStatementDataList = null;
		CheckLimitOrderBO dataBo = null;
		Connection connOra = null;
		CheckLimitOrderBO bo = null;
		CheckLimitOrderDAO dao = null;
		Connection conn = null;
		boolean result = false;
		MCustInfo cust = null;
		
		try {
			
			connOra = ConnectionUtils.getConnection(Constant.ORACLE, Constant.FROM);
			conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO);
			
			System.out.println("Call inquiryAccountStatementTest : Begin");
			System.out.println(">cifNo = "+wsBo.getCifNo());
			System.out.println(">accNo = "+wsBo.getMarginAccNo());
			System.out.println(">startDate = "+wsBo.getStartDate());
			System.out.println(">endDate = "+wsBo.getEndDate());
			
			dao = new CheckLimitOrderDAO();
			bo = new CheckLimitOrderBO();
			
			//-- Parameters
			bo.setInCifNo(wsBo.getCifNo());	//-- cifNo
			bo.setCreateUser(wsBo.getUser());	//-- createUser
			
			bo = dao.getTDealsDataFromRETAD(bo, connOra);
			
			cust = new MCustInfo();
			cust.setMarginAccNo(wsBo.getMarginAccNo());	//-- accNo
			bo.setmCustInfo(cust);
			
			
			//-- Check t_acc_pos
			System.out.println("Check t_acc_pos >>> processing...");
			bo = dao.getTAccPos(bo, conn);
			bo.setAsOfDate(commonUtils.convertUtilDatetoSQLDate(commonUtils.getCurrentDate()));
			bo.setOutsBal(new BigDecimal(0));
			
			System.out.println("Check t_acc_pos <<< finish : "+bo.gettAccPos());
			if(bo.gettAccPos() != null){
				dao.updateTAccPos(bo,conn);
				System.out.println("Update TAccPos success!");
			}else{
				dao.insertTAccPos(bo,conn);
				System.out.println("Insert TAccPos success!");
			}
			
			
			wsBo = inquiryAccountStatement(wsBo);
			wsBo.setAccountStatementDataList(wsBo.getAccountStatementDataList()); 
			accountStatementDataList = wsBo.getAccountStatementDataList();
			
			System.out.println(">"+accountStatementDataList +" size = "+((accountStatementDataList!=null)?accountStatementDataList.size():"-1"));
			if(accountStatementDataList != null){
				
				for(int i=0; i<accountStatementDataList.size(); i++){
					dataBo = accountStatementDataList.get(i);
					dataBo.setmCustInfo(bo.getmCustInfo());
					dataBo.settAccPos(bo.gettAccPos());
					System.out.println("accPos : "+dataBo.gettAccPos());
					System.out.println("["+i+"] : "+dataBo.getTransRefNo());
					
					//-- Check t_acc_tran
					dataBo = dao.getTAccTrans(dataBo, conn);
					System.out.println("Check TAccTrans : "+dataBo.gettAccTran());
					if(dataBo.gettAccTran() == null){
						result = dao.insertTAccTran(dataBo, conn);
						System.out.println("Insert TAccTran : "+result);
					}
				}
			}
			
			System.out.println("Call inquiryAccountStatementTest : End");
			
			conn.commit();
			
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		
		}finally{
			if(connOra != null){
				connOra.close();
			}
			if(conn != null){
				conn.close();
			}
		}
		
			
	}
	
	public static void main(String[] args){
		InquiryAccountWSBO wsBo = new InquiryAccountWSBO();
		InquiryAccountWS ws = new InquiryAccountWS();
		
		try{
			wsBo.setCifNo("199922");
			wsBo.setMarginAccNo("1133802");
			wsBo.setStartDate("20100222");
			wsBo.setEndDate("20131211");
			wsBo.setUser("TEST");
			
			ws.inquiryAccountStatementTest(wsBo);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
